package zuoYe.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


import zuoYe.entiy.roomPojo;

public class roomDao extends BaseDAO {
    BaseDAO baseDAO = new BaseDAO();
    public  List<roomPojo> getList(int index,int size) {

        List<roomPojo> roomList = new ArrayList<>();
        PreparedStatement statement = null;
        ResultSet set = null;
        Connection connection = getConnection();
        String sql = "select *\n" +
                "from hotel a join hotel_db.room r on a.id = r.hotel_id\n" +
                "limit ?,?; ";
        if (connection != null) {
            try {
                statement = connection.prepareStatement(sql);
                statement.setObject(1,index);
                statement.setObject(2,size);
                set = statement.executeQuery();
                while (set.next()) {
                    roomPojo room = new roomPojo();
                    room.setName(set.getString("name"));
                    room.setIp(set.getString("ip"));
                    room.setIntroduce(set.getString("introduce"));
                    room.setRoom_name(set.getString("room_name"));
                    room.setId(set.getInt("id"));
                    room.setHotel_id(set.getInt("r.id"));
                    room.setPrice(set.getDouble("price"));
                    roomList.add(room);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                allCol2(connection, statement, set);
            }
            return roomList;
        }
        return null;
    }

    public  int count() throws Exception {
        int count = 0;
        String sql = "select\n" +
                "count(r.id)\n" +
                "from hotel a join hotel_db.room r on a.id = r.hotel_id;";
        //创建连接串
        ResultSet set = baseDAO.getSet(sql, null);
        if (set.next()) {
            count = set.getInt(1);
        }
        return count % 3 == 0 ? count / 3 : count / 3 + 1;
    }
}
